import { defineStore } from 'pinia'
import { GetUserList, ChangeUserStatus, DeleteUser, ResetUserPassword, CreateUser, UpdateUser } from '@/api/user'

export const useUserStore = defineStore('user', () => {
    function getUserListAction(requestData) {
        return new Promise((resolve, reject) => {
            GetUserList(requestData).then(response => {
                resolve(response)
            }).catch(error => {
                reject(error)
            })
        })
    }

    function changeUserStatusAction(requestData) {
        const { id, data } = requestData
        return new Promise((resolve, reject) => {
            ChangeUserStatus(id, data).then(response => {
                resolve(response)
            }).catch(error => {
                reject(error)
            })
        })
    }

    function deleteUserAction(requestData) {
        const { id, data } = requestData
        return new Promise((resolve, reject) => {
            DeleteUser(id, data).then(response => {
                resolve(response)
            }).catch(error => {
                reject(error)
            })
        })
    }

    function resetUserPasswordAction(requestData) {
        const { id, data } = requestData
        return new Promise((resolve, reject) => {
            ResetUserPassword(id, data).then(response => {
                resolve(response)
            }).catch(error => {
                reject(error)
            })
        })
    }

    function createUserAction(requestData) {
        return new Promise((resolve, reject) => {
            CreateUser(requestData).then(response => {
                resolve(response)
            }).catch(error => {
                reject(error)
            })
        })
    }

    function updateUserAction(requestData) {
        return new Promise((resolve, reject) => {
            const { id, data } = requestData
            UpdateUser(id, data).then(response => {
                resolve(response)
            }).catch(error => {
                reject(error)
            })
        })
    }

    return { getUserListAction, changeUserStatusAction, deleteUserAction, resetUserPasswordAction, createUserAction, updateUserAction }
})